#!/bin/env ruby
#
#auxiliaries
#
#Dir.entries(Dir.pwd).each do |e|
#  puts e
#end
#

CHANNEL = "chan"
MAX_CAP = 500

def get_exist_pics()
  exist_pics = []
  Dir[File.join('**', CHANNEL + '_*.{jpg,jpeg,png,gif}')].each { |e|
    tmp = File.basename(e).scan(/[0-9]+/)[0]
    #exist_pics <<
    exist_pics << Integer(tmp)
  }
  #puts exist_pics
  exist_pics.sort!()
  puts "last is: #{exist_pics[-1]}"
  return exist_pics
end

def get_dirs()
  dirs = []
  # only in this dir, not recursive
  Dir['pic_' + CHANNEL + '_[0-9]*'].each { |e|
    if File.directory?(e)
      dirs << Integer(e)
    end
  }
  dirs.sort!()
  return dirs
end

#ranges = Array.new
#sp = 0
#ep = exist_pics[0]
#exist_pics.size().times { |i|
#  if sp==ep:
#}
    #
# check directory missing files
def check_dir_integ(d)
  # original list
  group = d.split('_')[2].to_i(16) * MAX_CAP
  out = Array.new
  group.upto(group + MAX_CAP - 1) { |e|
    out << e
  }
  # actual list
  exist_pics = Array.new
  Dir[File.join(d, CHANNEL + '_*.{jpg,jpeg,png,gif}')].each { |e|
    tmp = File.basename(e).scan(/[0-9]+/)[0]
    #exist_pics <<
    exist_pics << Integer(tmp)
  }
  exist_pics.sort!
  # sub
  tmp = (out - exist_pics)
  puts tmp
  puts "#{tmp.size} pics not yet downed."
  return tmp
end

#check_dir_integ "pic_chan_0005f"
#

eps = get_exist_pics()
ds = {}
eps.each { |p|
  ds[p / MAX_CAP] = 1
}
k = ds.keys.sort
k.each { |d| puts sprintf("%06x : (%d - %d)", d, d * MAX_CAP, (d + 1) * MAX_CAP - 1) }
